using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace WeatherMcpServer.Data.Entities;

/// <summary>
/// 预警记录表（主表）。记录一次天气预警的核心信息及生命周期状态。
/// </summary>
[Index(nameof(Type))]
[Index(nameof(Severity))]
[Index(nameof(Status))]
[Index(nameof(CreatedAt))]
public class WeatherAlert
{
    [Key]
    /// <summary>主键ID</summary>
    public long Id { get; set; }

    [MaxLength(50)]
    /// <summary>数据来源（如 AMAP）。用于区分多数据源场景</summary>
    public string Source { get; set; } = "AMAP"; // 数据来源标记

    /// <summary>天气类型（高温、暴雨等）</summary>
    public WeatherType Type { get; set; }

    /// <summary>预警严重等级</summary>
    public AlertSeverity Severity { get; set; }

    [MaxLength(32)]
    /// <summary>区域编码（如高德 adcode）</summary>
    public string? RegionCode { get; set; } // adcode 或其他行政区标识

    [MaxLength(128)]
    /// <summary>区域名称（人类可读）</summary>
    public string? RegionName { get; set; }

    /// <summary>最高气温（℃）</summary>
    public double? TemperatureMax { get; set; }
    /// <summary>最低气温（℃）</summary>
    public double? TemperatureMin { get; set; }
    /// <summary>降雨量（毫米）</summary>
    public double? RainMm { get; set; }
    /// <summary>风力等级（可存 Beaufort 或来源原值）</summary>
    public double? WindLevel { get; set; }

    [MaxLength(512)]
    /// <summary>预警描述/提示信息</summary>
    public string? Message { get; set; }

    /// <summary>处理状态</summary>
    public AlertStatus Status { get; set; } = AlertStatus.New;

    /// <summary>创建时间（UTC）</summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    /// <summary>确认时间（UTC）</summary>
    public DateTime? AcknowledgedAt { get; set; }
    /// <summary>解除/处理完成时间（UTC）</summary>
    public DateTime? ResolvedAt { get; set; }
}

/// <summary>
/// 预警规则表。定义预警触发条件、区域范围与严重程度。
/// </summary>
[Index(nameof(IsEnabled))]
[Index(nameof(Type))]
public class WeatherRule
{
    [Key]
    /// <summary>主键ID</summary>
    public long Id { get; set; }

    /// <summary>是否启用规则</summary>
    public bool IsEnabled { get; set; } = true;

    /// <summary>规则作用的天气类型</summary>
    public WeatherType Type { get; set; }

    // 条件类型：>=、<=、区间、=
    /// <summary>比较方式（>=、<=、区间、=）</summary>
    public RuleConditionType ConditionType { get; set; }

    // 阈值
    /// <summary>阈值1（如温度阈值或区间下限）</summary>
    public double? Threshold1 { get; set; }
    /// <summary>阈值2（区间上限，非区间条件可为空）</summary>
    public double? Threshold2 { get; set; }

    [MaxLength(32)]
    /// <summary>区域编码过滤（可选）</summary>
    public string? RegionCode { get; set; } // 可选：限定区域

    [MaxLength(256)]
    /// <summary>规则描述（便于配置与审阅）</summary>
    public string? Description { get; set; }

    /// <summary>该规则触发时建议的预警严重等级</summary>
    public AlertSeverity Severity { get; set; } = AlertSeverity.Moderate;

    /// <summary>规则创建时间（UTC）</summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 预警动作日志。记录针对某条预警所采取的通知/抑制/升级动作。
/// </summary>
[Index(nameof(AlertId))]
public class WeatherAlertActionLog
{
    [Key]
    /// <summary>主键ID</summary>
    public long Id { get; set; }

    /// <summary>关联的预警ID</summary>
    public long AlertId { get; set; }

    /// <summary>动作类型（通知、抑制、升级）</summary>
    public AlertActionType ActionType { get; set; }

    /// <summary>通知渠道（UI/Email/SMS/Webhook）</summary>
    public AlertActionChannel Channel { get; set; }

    [MaxLength(256)]
    /// <summary>动作目标（邮箱/手机号/URL 等）</summary>
    public string? Target { get; set; } // 比如邮箱/手机号/URL

    [MaxLength(512)]
    /// <summary>执行结果摘要（成功/失败原因等）</summary>
    public string? Result { get; set; }

    /// <summary>记录时间（UTC）</summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 天气数据源抓取日志。记录外部接口调用与错误信息，便于追踪与审计。
/// </summary>
[Index(nameof(Source))]
[Index(nameof(RequestedAt))]
public class WeatherSourceFetchLog
{
    [Key]
    /// <summary>主键ID</summary>
    public long Id { get; set; }

    [MaxLength(50)]
    /// <summary>数据来源（如 AMAP）</summary>
    public string Source { get; set; } = "AMAP";

    [MaxLength(256)]
    /// <summary>请求URL（截断保存，避免过长）</summary>
    public string? RequestUrl { get; set; }

    /// <summary>HTTP 状态码</summary>
    public int StatusCode { get; set; }

    [MaxLength(1024)]
    /// <summary>错误信息（若有）</summary>
    public string? Error { get; set; }

    /// <summary>请求时间（UTC）</summary>
    public DateTime RequestedAt { get; set; } = DateTime.UtcNow;
}
